package com.hvgroup.modules.sanic.web;

import com.hvgroup.annotation.Log;
import com.hvgroup.annotation.rest.AnonymousPostMapping;
import com.hvgroup.modules.sanic.domain.dto.QaMessageDto;
import com.hvgroup.modules.sanic.domain.dto.QaSessionDto;
import com.hvgroup.modules.sanic.domain.dto.UserQaRecordDto;
import com.hvgroup.modules.sanic.service.ConvService;
import com.hvgroup.utils.PageResult;
import com.hvgroup.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @migratedFrom Python
 * @source controllers/user_service_api.py
 * @originalBlueprint user_service
 * @description 用户服务控制器，负责处理用户的聊天记录查询、删除操作，以及提交对话反馈。
 */

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/conv")
@Api(tags = "系统:会话管理")
public class ConvController {

    private final ConvService convService;
//    private final UserService userService;

    /**
     * 只会返回列表信息
     * @param request
     * @return
     */
    @Log("查询用户聊天记录")
    @ApiOperation("查询用户聊天记录")
//    @PostMapping("/query_user_record")
    @AnonymousPostMapping("/query_user_record")
//    @PreAuthorize("@el.check('timing:add')")
    public ResponseEntity<List<Map<String, List<QaSessionDto>>>> queryUserRecord(@RequestBody Map<String, String> request) {
//        int page = Integer.parseInt( request.getOrDefault("page", "1"));
//        int limit = Integer.parseInt( request.getOrDefault("limit", "1"));
//        Pageable pageable = PageRequest.of(page - 1, limit);

        long userId = SecurityUtils.getCurrentUserId();
        // todo userId
//        long userId = Long.parseLong(request.get("userId"));

//        Map<String, Object> map = new HashMap<>();
//        map.put("userId", userId);
//        map.put("name", request.get("name"));
        return new ResponseEntity<>(convService.groupSessions(userId, request), HttpStatus.OK);
    }

    /**
     * 只会返回列表信息
     *
     * @param request
     * @return
     */
    @Log("搜索用户会话名称")
    @ApiOperation("搜索用户会话名称")
//    @PostMapping("/query_user_record")
    @AnonymousPostMapping("/search_user_record")
//    @PreAuthorize("@el.check('timing:add')")
    public ResponseEntity<List<QaSessionDto>> searchUserRecord(@RequestBody Map<String, String> request) {

        // todo userId
        long userId = SecurityUtils.getCurrentUserId();
//        long userId = Long.parseLong(request.get("userId"));

//        Map<String, Object> map = new HashMap<>();
//        map.put("userId", userId);
//        map.put("name", request.get("name"));
        return new ResponseEntity<>(convService.searchUserRecord(userId, request), HttpStatus.OK);
    }


    @Log("查询用户聊天记录详情")
    @ApiOperation("查询用户聊天记录详情")
//    @PostMapping("/query_user_record")
    @AnonymousPostMapping("/query_user_record_detail")
//    @PreAuthorize("@el.check('timing:add')")
    public ResponseEntity<List<QaMessageDto>> queryUserRecordDetail(@RequestBody Map<String, String> request) {
//        int page = Integer.parseInt( request.getOrDefault("page", "1"));
//        int limit = Integer.parseInt( request.getOrDefault("limit", "1"));
//        Pageable pageable = PageRequest.of(page - 1, limit);

        long userId = SecurityUtils.getCurrentUserId();
        // todo userId
//        long userId = Long.parseLong(request.get("userId"));
        long chatId = Long.parseLong(request.get("chatId"));

//        Map<String, Object> map = new HashMap<>();
//        map.put("userId", userId);
//        map.put("name", request.get("name"));
        return new ResponseEntity<>(convService.getMessageList(userId, chatId), HttpStatus.OK);
    }


    @Log("删除用户问答记录")
    @ApiOperation("删除用户问答记录")
    @PostMapping("/delete_user_record")
//    @PreAuthorize("@el.check('timing:add')")
    public ResponseEntity<PageResult<UserQaRecordDto>> deleteUserRecord(@RequestBody Map<String, List<Long>> request) {

        List<Long> recordIds = request.get("record_ids");
        long userId = SecurityUtils.getCurrentUserId();

        convService.deleteUserRecord(userId, recordIds);
        return new ResponseEntity<>(HttpStatus.OK);
    }


    @Log("用户反馈")
    @ApiOperation("用户对大模型对话结果的反馈评分")
    @AnonymousPostMapping("/dify_fead_back")
//    @PreAuthorize("@el.check('timing:add')")
    public ResponseEntity<Object> difyFeadBack(@RequestBody Map<String, String> request) {


        long userId = SecurityUtils.getCurrentUserId();
//        long userId = Long.parseLong(request.get("userId"));

        Object o = convService.sendDifyFeedback(request, userId);
        return new ResponseEntity<>(o, HttpStatus.OK);
    }

}
